package com.hailong.zyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hailong.zyt.beans.Course;
import com.hailong.zyt.beans.Homework;
import com.hailong.zyt.beans.HomeworkStudent;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.mapper.CourseMapper;
import com.hailong.zyt.mapper.HomeworkMapper;
import com.hailong.zyt.mapper.HomeworkStudentMapper;
import com.hailong.zyt.mapper.StudentMapper;
import com.hailong.zyt.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hailong
 * @since 2021-05-17
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private HomeworkStudentMapper homeworkStudentMapper;

    @Override
    public List<Course> getOptionalCourses(Long stuId) throws ParseException {
        List<Course> all = courseMapper.selectOptionalByStuId(stuId);
        List<Course> courses = new ArrayList<>();
        Date nowDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        for(Course course : all) {
            Date start = format.parse(course.getCouStart()), end = format.parse(course.getCouEnd());
            if(nowDate.after(start) && nowDate.before(end)){
                courses.add(course);
            }
        }
        return courses;
    }

    @Override
    public void getGradeList(Model model, Long couId, Long teaId) {
        Map<String, Object> map = new HashMap<>();
        map.put("cou_id", couId);
        map.put("tea_id", teaId);
        List<Homework> homeworks = homeworkMapper.selectByMap(map);

        //查选了这门课的所有学生
        List<Student> students = studentMapper.selectStudentsByCouId(couId, teaId);

        Map<String, Object> map1 = new HashMap<>();
        for(Student student : students){

            float sum = 0;
            student.setUnFinished(0);
            student.setHws(new ArrayList<HomeworkStudent>());
            student.setMaxMark(0);

            map1.put("stu_id", student.getStuId());
            for(Homework homework : homeworks){
                map1.put("hw_id", homework.getHwId());
                HomeworkStudent homeworkStudent = homeworkStudentMapper.selectOne(new QueryWrapper<HomeworkStudent>().allEq(map1));
                if(homeworkStudent != null){
                    homeworkStudent.setHomework(homework);
                    if(homeworkStudent.getHwsMark() != null){
                        sum += homeworkStudent.getHwsMark();
                        if(student.getMaxMark() < homeworkStudent.getHwsMark()) student.setMaxMark(homeworkStudent.getHwsMark());
                    }
                }
                else{
                    homeworkStudent = new HomeworkStudent();
                    homeworkStudent.setHwsMark((float) -1);
                    student.setUnFinished(student.getUnFinished() + 1);
                }
                student.getHws().add(homeworkStudent);
            }
            if(homeworks.size() != 0) student.setAvgMark(sum / homeworks.size());
            else student.setAvgMark(0);
        }

        model.addAttribute("homeworks", homeworks);
        model.addAttribute("students", students);
    }
}
